package com.css.fxfzypg.modules.resultsManagement.repository.impl;

import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.resultsManagement.entity.MapManagementEntity;
import com.css.fxfzypg.modules.resultsManagement.entity.MapType;
import com.css.fxfzypg.modules.resultsManagement.repository.MapTypeRepository;
import com.css.fxfzypg.util.PlatformObjectUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class MapTypeRepositoryimpl implements MapTypeRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void saveMapType(MapType mapType) {
        String sql = "insert into map_type_management (id,map_type,dict_type,dict_code,report_type,enable_flag,del_flag,create_user,create_time) values " +
                "(:id,:mapType,:dictType,:dictCode,:reportType,:enableFlag,:delFlag,:createUser,:createTime)";
        BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(mapType);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public MapType getById(String id) {
        String sql = "select * from map_type_management where id='" + id + "' and del_flag ='" + YNEnum.N.toString() + "'";
        MapType mapType = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(MapType.class));
        return mapType;
    }

    @Override
    public void deleteById(String id, Timestamp timestamp) {
        String sql = "update map_type_management set del_flag = '" + YNEnum.Y + "' , update_time = '" + timestamp + "' " + " where id ='" + id + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public MapType update(MapType mapType) {
        String sql = "update map_type_management set map_type=:mapType,dict_type=:dictType,dict_code=:dictCode,report_type=:reportType," +
                "enable_flag=:enableFlag,del_flag=:delFlag,update_user=:updateUser," +
                "update_time=:updateTime where id=:id";
        SqlParameterSource source = new BeanPropertySqlParameterSource(mapType);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return mapType;
    }

    @Override
    public Map<String, Object> queryMaps(MapType mapType, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from map_type_management ");
        StringBuilder whereSql = new StringBuilder("where del_flag = '" + YNEnum.N.toString() + "' ");
        if (!PlatformObjectUtils.isEmpty(mapType.getReportType())) {
            whereSql.append(" and report_type = '" + mapType.getReportType() + "' ");
        }
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        StringBuilder sql1 = sql.append(whereSql);
        Integer total = jdbcTemplate.query(sql1.toString(), new BeanPropertyRowMapper<>(MapType.class)).size();
        String sql2 = sql1.append(orderSql).append(limitSql).toString();
        List<MapType> maps = jdbcTemplate.query(sql2, new BeanPropertyRowMapper<>(MapType.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", maps);
        return map;
    }

    @Override
    public MapType getByMapType(String mapType, String id,String reportType) {
        String sql = "select * from map_type_management where map_type='" + mapType + "' and del_flag ='" + YNEnum.N.toString() + "' and id !='" + id + "' and report_type = '"+reportType+"'";
        try {
            MapType mapType1 = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(MapType.class));
            return mapType1;
        } catch (DataAccessException e) {
            return null;
        }

    }

    @Override
    public List<MapType> getByReportType(String reportType) {
        String sql = "select * from map_type_management where report_type ='" + reportType + "' and del_flag ='" + YNEnum.N.toString() + "'";
        List<MapType> mapTypes = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(MapType.class));
        return mapTypes;
    }

}
